package com.tanhua.admin.service;


import com.google.common.util.concurrent.AtomicDouble;
import com.tanhua.admin.mapper.LogMapper;
import com.tanhua.model.vo.CountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;


@Service
public class DashboardService {

    @Autowired
    private LogMapper logMapper;

    public CountVo statistics() {
        //创建vo对象
        CountVo countVo = new CountVo();
        //统计累计用户
        Integer count = logMapper.cumulativeUsers();
        countVo.setCumulativeUsers(count);
        //过去30天活跃用户
        String currentDay = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        Integer current30Count = logMapper.activePassMonth(currentDay);
        countVo.setActivePassMonth(current30Count);
        //过去7天活跃用户
        Integer current7Count = logMapper.activePassWeek(currentDay);
        countVo.setActivePassWeek(current7Count);
        //今日新增用户
        Integer todayCount = logMapper.newUsersToday(currentDay);
        countVo.setNewUsersToday(todayCount);
        //昨日新增用户
        Integer yesterdayCount = logMapper.yesterdayCount(currentDay);
        if (yesterdayCount==0){
            countVo.setActiveUsersTodayRate(0);
        }else{
            //计算新增用户涨跌率
            AtomicInteger atomicInteger = new AtomicInteger((todayCount - yesterdayCount) / yesterdayCount * 100);
            countVo.setActiveUsersTodayRate(atomicInteger.intValue());
        }

        //今日登录次数
        Integer todayLoginCount=logMapper.loginTimesToday(currentDay);
        countVo.setLoginTimesToday(todayLoginCount);
        //昨日登录次数
        Integer yesterdayLoginCount=logMapper.yesterdayLoginCount(currentDay);
        //计算登录用户涨跌率
        if (yesterdayLoginCount==0){
            countVo.setLoginTimesTodayRate(0);
        }else{
            //计算新增用户涨跌率
            AtomicInteger atomicInteger1 = new AtomicInteger((todayLoginCount - yesterdayLoginCount) / yesterdayLoginCount * 100);
            countVo.setLoginTimesTodayRate(atomicInteger1.intValue());
        }
        //今日活跃用户
        Integer activeCount=logMapper.activeUsersToday(currentDay);
        countVo.setActiveUsersToday(activeCount);
        //昨日活跃用户
        Integer yesterdayActiveCount=logMapper.yesterdayActive(currentDay);
        //计算活跃用户涨跌率
        if (yesterdayActiveCount==0){
            countVo.setLoginTimesTodayRate(0);
        }else{
            //计算新增用户涨跌率
            AtomicInteger atomicInteger2 = new AtomicInteger((activeCount - yesterdayActiveCount) / yesterdayActiveCount * 100);
            countVo.setLoginTimesTodayRate(atomicInteger2.intValue());
        }
        return countVo;
    }
}
